// pages/radioOption/radio.js
Page({
  data: {
    title:'',
    type:'',
    options:[],
    windowHeight:0,
    windowWidth:0,
    input_type:'',
    charCode:[
      'A','B','C','D','E','F','G','H'
    ]
  },


  input(e){
    var options = this.data.options;
    // console.log(e);
    options[parseInt(e.target.id)].option_value=e.detail.value;
    // console.log(e.detail.value);
    this.setData({
      options:options
    })
  },
  onReady: function () {
    wx.getSystemInfo({
      success: res => {
        this.setData({
          windowWidth: res.windowWidth,
          windowHeight: res.windowHeight
        })
        console.log(res.windowWidth);
      }
    })
  },

  titleInput(e){
    this.setData({
      title:e.detail.value
    })
  },

  delInput(){
    var options = this.data.options;
    options.pop();
    this.setData({
      options:options,
    })
  },

  addInput(){
    var options = this.data.options;
    var oid;
    if(options.length == 8){
      wx.showToast({
        title: '最多8个选项',
        icon: 'error',
        duration: 2000
       })
    }else{
      if(this.data.options.length == 0){
        oid = 0;
      }else{
        oid=options.length;
      }
      var newData={
        option_id:this.data.charCode[oid],
        option_value:""
      };
      this.data.options.push(newData);
      this.setData({
        options:options,
      })
    }
  },

  save(){
    if(this.data.title==''||this.data.title==null)
    {
      wx.showToast({
        title: '请输入题目！',
        icon: 'none',
        duration: 1500
     })
     return;
    }
    if(this.data.options.length==0||this.data.options==null)
    {
      wx.showToast({
        title: '请至少输入一个选项！',
        icon: 'none',
        duration: 1500
     })
     return;
    }
    // console.log(this.data);
    var app = getApp();
    this.data.type = "radio";
    this.data.input_type = "text"
    app.globalData.tmpquestion = this.data
    app.globalData.edited = true;    

    wx.navigateBack({
      delta: 1,
    })
  }
})